import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import { toFixed } from '@/utils/format';


const capPush = (arr: number[], v: number, cap = 120) => {
  arr.push(v)
  if (arr.length > cap) arr.shift()
}


export const motorSlice = createSlice({
  name: 'motor',
  initialState: {
    ecsState: {
      voltage1: [],
      voltage2: [],
      voltage3: [],
      voltage4: [],
      voltage5: [],
      voltage6: [],
      electricity1: [],
      electricity2: [],
      temp1: [],
      temp2: [],
      cupTemp: [],
      charge: 0,
      dac: 0,
    },
    motorState: {
      motorARun: [],
      motorBRun: [],
      motorAControl: [],
      motorBControl: [],
      motorAPWMDuty: [],
      motorBPWMDuty: [],
      motorAPWMFrequency: [],
      motorBPWMFrequency: [],
      motorARate: [],
      motorBRate: [],
      motorAProtection: [],
      motorBProtection: [],
      motorACoder: [],
      motorBCoder: [],
    }
  },
  reducers: {
    pushECSState: (state, { payload }) => {
      // capPush(state.ecsState.voltage1, toFixed(payload.voltage1));
      // capPush(state.ecsState.voltage2, toFixed(payload.voltage2));
      // capPush(state.ecsState.voltage3, toFixed(payload.voltage3));
      // capPush(state.ecsState.voltage4, toFixed(payload.voltage4));
      // capPush(state.ecsState.voltage5, toFixed(payload.voltage5));
      // capPush(state.ecsState.voltage6, toFixed(payload.voltage6));

      state.ecsState.voltage5 = payload.voltage5
      capPush(state.ecsState.electricity1, toFixed(payload.electricity1));
      capPush(state.ecsState.electricity2, toFixed(payload.electricity2));
      capPush(state.ecsState.temp1, toFixed(payload.temp1));
      capPush(state.ecsState.temp2, toFixed(payload.temp2));
      capPush(state.ecsState.cupTemp, toFixed(payload.cupTemp));
      state.ecsState.charge = payload.charge;
      state.ecsState.dac = payload.dac;
    },
    pushMotorState: (state, { payload }) => {
      state.motorState.motorARun = payload.motorARun;
      state.motorState.motorBRun = payload.motorBRun;
      state.motorState.motorAControl = payload.motorAControl;
      state.motorState.motorBControl = payload.motorBControl;
      capPush(state.motorState.motorAPWMDuty, toFixed(payload.motorAPWMDuty), 10);
      capPush(state.motorState.motorBPWMDuty, toFixed(payload.motorBPWMDuty), 10);
      // state.motorState.motorAPWMDuty = payload.motorAPWMDuty;
      // state.motorState.motorBPWMDuty = payload.motorBPWMDuty;

     state.motorState.motorAPWMFrequency = payload.motorAPWMFrequency;
     state.motorState.motorBPWMFrequency = payload.motorBPWMFrequency;
      // capPush(state.motorState.motorAPWMDuty, payload.motorAPWMDuty);
      // capPush(state.motorState.motorBPWMDuty, payload.motorBPWMDuty);
      // capPush(state.motorState.motorAPWMFrequency, payload.motorAPWMFrequency);
      // capPush(state.motorState.motorBPWMFrequency, payload.motorBPWMFrequency);
      capPush(state.motorState.motorARate, payload.motorARate);
      capPush(state.motorState.motorBRate, payload.motorBRate);
      state.motorState.motorAProtection = payload.motorAProtection;
      state.motorState.motorBProtection = payload.motorBProtection;
      state.motorState.motorACoder = payload.motorACoder;
      state.motorState.motorBCoder = payload.motorBCoder;

      //  console.log('pushMotorState', state.motorState, state.motorState.motorARun)
    },
  }
})

const { pushECSState, pushMotorState } = motorSlice.actions
export { pushECSState, pushMotorState }

export default motorSlice.reducer
